package au.edu.uq.itee.engg2800.team19;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Observable;

import javax.swing.AbstractButton;
import javax.swing.GrayFilter;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class ToggleStartStop extends JPanel implements ActionListener,java.util.Observer,Observer {
	private RS232Conn conn;
	private FrequenciesReceived freqs;
	private JButton bStart;
	private JLabel label1;
	private JButton bStop;
	private JButton bReset;
	private JLabel connectedLabel;
	private ImageIcon imgConnected=new ImageIcon("Images//Connection-Check.gif");
	private ImageIcon disImgConnected=new ImageIcon
						(GrayFilter.createDisabledImage(imgConnected.getImage()));
	public JHyperProtocol sendprot;
	public JHyperProtocol recprot;;
	public ToggleStartStop(RS232Conn conn,FrequenciesReceived freq){
		this.conn=conn;
		this.freqs=freq;

		conn.addObserver(this);
		connectedLabel = new JLabel(disImgConnected);
		connectedLabel.setVisible(true);
		recprot=new JHyperProtocol(freqs);
		sendprot=new JHyperProtocol(freqs);
		bReset = new JButton("Reset");
		bReset.setVerticalTextPosition(AbstractButton.CENTER);
		bReset.setHorizontalTextPosition(AbstractButton.LEADING);
		bReset.setMnemonic(KeyEvent.VK_R);
		bReset.setEnabled(true);
		bReset.setActionCommand("reset");

	    bStart = new JButton("Start Sweep");
	    bStart.setVerticalTextPosition(AbstractButton.CENTER);
	    bStart.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
	    bStart.setMnemonic(KeyEvent.VK_S);
	    bStart.setEnabled(false);
	    bStart.setActionCommand("disable");

	    label1 = new JLabel("Sweep Running");
	    label1.setVisible(false);

	    bStop = new JButton("Stop Sweep");
	    bStop.setVerticalTextPosition(AbstractButton.CENTER);
	    bStop.setHorizontalTextPosition(AbstractButton.LEADING);
	    bStop.setMnemonic(KeyEvent.VK_E);
	    bStop.setActionCommand("enable");
	    bStop.setEnabled(false);

	    //Listen for actions on buttons 1 and 3.
	    bStart.addActionListener(this);
	    bStop.addActionListener(this);
	    bReset.addActionListener(this);

	    bStart.setToolTipText("Click this button to start "
	                      + "the sweep.");
	    bStop.setToolTipText("Click this button to stop "
	                      + "the sweep.");
	    bReset.setToolTipText("Click this button to Reset "
                + "the sweep.");

	   this.add(bReset);
	    this.add(bStart);
	   this.add(bStop);
	   this.add(label1);
	   this.add(connectedLabel);


	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if ("disable".equals(e.getActionCommand())) {
	            label1.setVisible(true);
	            bStart.setEnabled(false);
	            bStop.setEnabled(true);
	            bReset.setEnabled(false);
	            DataSender.send(sendprot.getMessage("0"));
	            //freqs.setOrdinalback();
	            freqs.setPlottable(true);


	        } else if ("enable".equals(e.getActionCommand())){
	        	label1.setVisible(false);
	            bStart.setEnabled(true);
	            bStop.setEnabled(false);
	            bReset.setEnabled(true);
	            DataSender.send(recprot.getMessage("1"));
	            freqs.setPlottable(false);

	        } else {

	        	freqs.setPlottable(true);
	        	for (int i=1;i<=8;i++){
	        		freqs.updateMap(0);
	        	}
	        	freqs.resetOrdinal();
	        	freqs.setPlottable(false);
	        }

	    }
	@Override
	public void update(Observable o, Object arg) {
		System.out.println("data recieved on serial");
		if (conn.isConnected){
			connectedLabel.setIcon(imgConnected);
			   bStart.setEnabled(true);
		}
		else{
			connectedLabel.setIcon(disImgConnected);
		}
	}
	@Override
	public void update(String messageText) {
	System.out.println("Got a Message");
	}
	}